<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('月评价供电所填报任务表列表')"/>
</head>
<style>
    td[data-field=status][data-content=NONE] {
        background-color: #bdc2c3;
    }
</style>
<body class="pear-container">
<!--<div class="layui-card">-->
<!--    <div class="layui-card-body">-->
<!--        <form class="layui-form" action="">-->
<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label">任务年月</label>-->
<!--                <div class="layui-input-inline">-->
<!--                    <input type="text" name="taskMonth" placeholder="" class="layui-input" id="taskMonth">-->
<!--                </div>-->
<!--                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit-->
<!--                        lay-filter="viewdeduction-query">-->
<!--                    <i class="layui-icon layui-icon-search"></i>-->
<!--                    查询-->
<!--                </button>-->
<!--                <button type="reset" class="pear-btn pear-btn-md">-->
<!--                    <i class="layui-icon layui-icon-refresh"></i>-->
<!--                    重置-->
<!--                </button>-->
<!--            </div>-->
<!--        </form>-->
<!--    </div>-->
<!--</div>-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="viewdeduction-table" lay-filter="viewdeduction-table"></table>
    </div>
</div>
</body>

<script type="text/html" id="content-event">
    {{#  if(d.type != 3){ }}
    <!--    <button class="pear-btn pear-btn-warming pear-btn-sm"-->
    <!--            lay-event="" title="填报帮助文档" onclick="viewrule('{{d.content}}')"><i class="layui-icon layui-icon-search"></i>-->
    <!--    </button>-->
    <button class="pear-btn pear-btn-primary pear-btn-sm"
            lay-event="" title="评价方式" onclick="viewrule('{{d.content}}')"><i
            class="layui-icon layui-icon-search"></i>
        评价方式
    </button>
    {{#  }else{ }}
    <span>{{d.content}}</span>
    {{#  } }}
</script>


<script type="text/html" id="viewdeduction-toolbar">

</script>

<script type="text/html" id="viewdeduction-bar">

</script>


<script type="text/html" id="viewdeduction-type">
    {{#if (d.dataState == '0') { }}
    <span class="layui-badge layui-bg-orange">开启</span>
    {{# }else if(d.dataState == '-1'){ }}
    <span class="layui-badge layui-bg-blue">关闭</span>
    {{# } }}
</script>

<th:block th:include="include :: footer"/>
<script th:inline="javascript" type="text/javascript">
    layui.use(['table', 'form', 'jquery'], function () {
        var table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;

        //执行一个laydate实例
        laydate.render({
            elem: '#taskMonth' //指定元素
            , type: 'month'
        })

        /*<![CDATA[*/
        var taskid = /*[[${taskid}]]*/ 20;
        /*]]>*/
        console.log(taskid)

        var MODULE_PATH = rootPath + "/MonthSupplytaskController/";
        var cols = [
            [
                {
                    title: '指标编号', field: 'num', align: 'center', width: 160, templet: function (res) {
                        return redline(res.num, res.isselfdeduct)
                    }
                },
                {
                    title: '指标名称', field: 'name', align: 'center', width: 700, templet: function (res) {
                        return redline(res.name, res.isselfdeduct)
                    }
                },
                {
                    title: '自评价填报值',
                    field: 'selfValue',
                    align: 'center',
                    width: 240,
                    templet: function (d) {
                        if (d.type === '3') {
                            return '<span >' + d.selfValue + '</span>';
                        } else {
                            console.log("扣分状态" + d.isselfdeduct)
                            if (d.tactics === '1' || d.tactics === '3') {
                                var selfValue = d.selfValue == null ? "" : d.selfValue
                                if (d.isauto === 1) {
                                    return '<input type="text" style="width: 80px;text-align:center" disabled="disabled" id = "' + d.id + '" value="' + selfValue + '"  data-old = "' + d.selfValue + '" data-num = "' + d.num + '" data-unit = "' + d.measuringUnit + '">'
                                        + '<span>' + d.measuringUnit + '</span>'
                                } else {
                                    return '<input type="text" style="width: 80px;text-align:center" disabled="disabled" id = "' + d.id + '" value="' + selfValue + '"  data-old = "' + d.selfValue + '" data-num = "' + d.num + '" data-unit = "' + d.measuringUnit + '">'
                                        + '<span>' + d.measuringUnit + '</span>'
                                }
                            } else if (d.tactics === '2') {
                                if (d.isauto === 1) {
                                    if (d.selfValue === "1") {
                                        return '<input type="radio" disabled="disabled" checked name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '"  title="是">' +
                                            '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否">';
                                    } else {
                                        return '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '" title="是">' +
                                            '<input type="radio" disabled="disabled" checked name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否">';
                                    }
                                } else {
                                    if (d.selfValue === "1") {
                                        return '<input type="radio"  disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '" title="是"  checked>' +
                                            '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否">';
                                    } else if (d.selfValue === "0") {
                                        return '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '" title="是"  >' +
                                            '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否" checked>';
                                    } else {
                                        return '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^1^^^' + d.num + '" title="是"  >' +
                                            '<input type="radio" disabled="disabled" name="radio' + d.id + '" value="' + d.id + '^^^' + d.selfValue + '^^^0^^^' + d.num + '" title="否" >';
                                    }
                                }
                            } else {
                                return '';
                            }
                        }
                    }
                },
                {
                    title: '评价方式及材料',
                    field: 'fkParentProjectId',
                    align: 'center',
                    width: 160,
                    templet: '#content-event',
                    merge: true
                },
                {title: '项目分值', field: 'score', align: 'center', merge: true},
                {title: '自评分', field: 'proscore', align: 'center', merge: true},
            ]
        ]

        //table表单列表
        table.render({
            elem: '#viewdeduction-table',
            url: MODULE_PATH + 'list4supplydeduction/' + taskid,
            page: true,
            method: 'get',
            cols: cols,
            skin: 'line',
            toolbar: '#viewdeduction-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }],
            done: function (res) {
                merge(this);
            }
        });

        //table图标事件绑定
        table.on('tool(viewdeduction-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        //table工具栏绑定
        table.on('toolbar(viewdeduction-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        //查询按钮绑定
        form.on('submit(viewdeduction-query)', function (data) {
            table.reload('viewdeduction-table', {where: data.field})
            return false;
        });

        //刷新按钮
        window.refresh = function () {
            table.reload('viewdeduction-table');
        }
    })

    function merge(myTable, skipFlag = true) {
        var tableBox = $(myTable.elem).next().children('.layui-table-box'),
            $main = $(tableBox.children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
            $fixLeft = $(tableBox.children('.layui-table-fixed-l').children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
            $fixRight = $(tableBox.children('.layui-table-fixed-r').children('.layui-table-body').children('table').children('tbody').children('tr').toArray().reverse()),
            cols = myTable.cols[0], mergeRecord = {};

        for (var i = 0; i < cols.length; i++) {
            var item3 = cols[i], field = item3.field;
            if (item3.merge) {
                var mergeField = [field];
                if (item3.merge !== true) {
                    if (typeof item3.merge == 'string') {
                        mergeField = [item3.merge]
                    } else {
                        mergeField = item3.merge
                    }
                }
                mergeRecord[i] = {mergeField: mergeField, rowspan: 1}
            }
        }

        $main.each(function (i) {
            for (var _recordIndex in mergeRecord) {
                var _mergeRecord = mergeRecord[_recordIndex];
                //检查前一个字段
                var _beforeMergeRecord = mergeRecord[_recordIndex - 1];
                //
                if (i === $main.length - 1 || (skipFlag && _beforeMergeRecord && _beforeMergeRecord.rowspan === 1) || isMaster(i, _mergeRecord)) {
                    $(this).children('[data-key$="-' + _recordIndex + '"]').attr('rowspan', _mergeRecord.rowspan);
                    $fixLeft.eq(i).children('[data-key$="-' + _recordIndex + '"]').attr('rowspan', _mergeRecord.rowspan);
                    $fixRight.eq(i).children('[data-key$="-' + _recordIndex + '"]').attr('rowspan', _mergeRecord.rowspan);
                    _mergeRecord.rowspan = 1;
                } else {
                    $(this).children('[data-key$="-' + _recordIndex + '"]').remove();
                    $fixLeft.eq(i).children('[data-key$="-' + _recordIndex + '"]').remove();
                    $fixRight.eq(i).children('[data-key$="-' + _recordIndex + '"]').remove();
                    _mergeRecord.rowspan += 1;
                }
            }
        })

        function isMaster(index, _mergeRecord) {
            var mergeField = _mergeRecord.mergeField;
            var dataLength = layui.table.cache[myTable.id].length;
            for (var i = 0; i < mergeField.length; i++) {

                if (layui.table.cache[myTable.id][dataLength - 2 - index][mergeField[i]]
                    !== layui.table.cache[myTable.id][dataLength - 1 - index][mergeField[i]]) {
                    return true;
                }
            }
            return false;
        }

        //查看评分细则
        window.viewrule = function (d) {
            layer.open({
                type: 1,
                title: '评价方式',
                area: ['500px', '500px'],
                // content: '<div><textarea name="description">'+d+'</textarea></div>'
                content: d
            });
        }
    }

    // 定义函数
    function redline(score, isedit) {
        if (isedit == 1) {
            return '<a class=""style="color:#ed2a4a">' + score + '</a>';
        } else {
            return score
        }
    }


</script>
</html>